﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Configuration; 

using BSC;
using EmrDatastructure;
using System.Web.Security;

namespace WcfEmrDAL
{
    public class EmrService : IEmrService
    {
        private ChannelFactory<IEmrService> factory = null;
        private IEmrService proxy = null;
        public EmrService(string endPoint)
        {
            EndpointAddress address = new EndpointAddress(endPoint);
            BasicHttpBinding binding = new BasicHttpBinding();
            factory = new ChannelFactory<IEmrService>(binding, address);
        }
        public EmrService()
        {
            if (ConfigurationManager.AppSettings.Count == 0 || ConfigurationManager.AppSettings["EmrWcfServiceEndPoint"] == null)
            {
                throw new Exception("Unable to located the EmrWcfServiceEndPoint setting in the configuration file");
            }
            EndpointAddress address = new EndpointAddress(ConfigurationManager.AppSettings["EmrWcfServiceEndPoint"]);
            BasicHttpBinding binding = new BasicHttpBinding();
            factory = new ChannelFactory<IEmrService>(binding, address);
        }
        private void CloseChannel()
        {
            if (factory != null && ((IChannel)proxy) != null)
            {
                if (((IChannel)proxy).State == CommunicationState.Opened)
                {
                    ((IChannel)proxy).Close();
                }
                else
                {
                    ((IChannel)proxy).Abort();
                }
            }
        }

        public void CreateEmrDemographic(EmrDemographic r)
        {
            try
            {
                proxy = factory.CreateChannel();
                proxy.CreateEmrDemographic(r);
            }
            finally
            {
                CloseChannel();
            }
        }
        public void UpdateEmrDemographic(EmrDemographic r)
        {
            try
            {
                proxy = factory.CreateChannel();
                proxy.UpdateEmrDemographic(r);
            }
            finally
            {
                CloseChannel();
            }
        }
        public EmrDemographic ReadEmrDemographic(string identificationNumber)
        {
            try
            {
                proxy = factory.CreateChannel();
                return proxy.ReadEmrDemographic(identificationNumber);
            }
            finally
            {
                CloseChannel();
            }
        }
        public IList<EmrDemographic> ReadListEmrDemographic(string howToSpec)
        {
            throw new NotImplementedException();
        }
        public IList<EmrDemographic> SearchList(string orLastName, string orFirstName, DateTime? orDateOfBirth)
        {
            try
            {
                proxy = factory.CreateChannel();
                return proxy.SearchList(orLastName, orFirstName, orDateOfBirth);
            }
            finally
            {
                CloseChannel();
            }
        }
        public void DeleteEmrDemographic(string identificationNumber)
        {
            try
            {
                proxy = factory.CreateChannel();
                proxy.DeleteEmrDemographic(identificationNumber);
            }
            finally
            {
                CloseChannel();
            }
        }

        public void CreateEmrProblem(EmrProblem r)
        {
            try
            {
                proxy = factory.CreateChannel();
                proxy.CreateEmrProblem(r);
            }
            finally
            {
                CloseChannel();
            }
        }
        public EmrProblem ReadEmrProblem(string id)
        {
            try
            {
                proxy = factory.CreateChannel();
                return proxy.ReadEmrProblem(id);
            }
            finally
            {
                CloseChannel();
            }
        }
        public void UpdateEmrProblem(EmrProblem r)
        {
            try
            {
                proxy = factory.CreateChannel();
                proxy.UpdateEmrProblem(r);
            }
            finally
            {
                CloseChannel();
            }
        }
        public void DeleteEmrProblem(int id)
        {
            try
            {
                proxy = factory.CreateChannel();
                proxy.DeleteEmrProblem(id);
            }
            finally
            {
                CloseChannel();
            }
        }
        public IList<EmrProblem> ReadListEmrProblem(string identificationNumber)
        {
            try
            {
                proxy = factory.CreateChannel();
                return proxy.ReadListEmrProblem(identificationNumber);
            }
            finally
            {
                CloseChannel();
            }
        }

        public void CreateEmrSmoking(EmrSmoking r)
        {
            try
            {
                proxy = factory.CreateChannel();
                proxy.CreateEmrSmoking(r);
            }
            finally
            {
                CloseChannel();
            }
        }
        public EmrSmoking ReadEmrSmoking(string id)
        {
            try
            {
                proxy = factory.CreateChannel();
                return proxy.ReadEmrSmoking(id);
            }
            finally
            {
                CloseChannel();
            }
        }
        public void UpdateEmrSmoking(EmrSmoking r)
        {
            try
            {
                proxy = factory.CreateChannel();
                proxy.UpdateEmrSmoking(r);
            }
            finally
            {
                CloseChannel();
            }
        }
        public void DeleteEmrSmoking(int id)
        {
            try
            {
                proxy = factory.CreateChannel();
                proxy.DeleteEmrSmoking(id);
            }
            finally
            {
                CloseChannel();
            }
        }
        ////////
        public bool ValidateUser(string userName, string password)
        {
            try
            {
                proxy = factory.CreateChannel();
                return proxy.ValidateUser(userName, password);
            }
            finally
            {
                CloseChannel();
            }
        }

        public MembershipCreateStatus CreateUser(string userName, string password, string email)
        {
            try
            {
                proxy = factory.CreateChannel();
                return proxy.CreateUser(userName, password, email);
            }
            finally
            {
                CloseChannel();
            }
        }

        public bool ChangePassword(string userName, string oldPassword, string newPassword)
        {
            try
            {
                proxy = factory.CreateChannel();
                return proxy.ChangePassword(userName, oldPassword, newPassword);
            }
            finally
            {
                CloseChannel();
            }
        }
    }
}
